import Vue from 'vue'
import VueRouter from 'vue-router'
import { getItem } from '@/utils/storage'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('@/views/login')
  },
  {
    path: '/home',
    name: 'home',
    redirect: '/welcome',
    component: () => import('@/views/home'),
    children: [
      {
        path: '/welcome',
        name: 'welcome',
        component: () => import('@/views/home/components/Welcome')
      },
      {
        path: '/users',
        name: 'users',
        component: () => import('@/views/user')
      },
      {
        path: '/roles',
        name: 'roles',
        component: () => import('@/views/power/Roles')
      },
      {
        path: '/rights',
        component: () => import('@/views/power/Rights')
      },
      {
        path: '/categories',
        component: () => import('@/views/goods/GoodsCate')
      },
      {
        path: '/params',
        component: () => import('@/views/goods/GoodsParams')
      },
      {
        path: '/goods',
        name: 'goods',
        component: () => import('@/views/goods/GoodsList')
      },
      {
        path: '/goods/add',
        name: 'goodsAdd',
        component: () => import('@/views/goods/AddGoods')
      },
      {
        path: '/orders',
        component: () => import('@/views/order/OrderList')
      },
      {
        path: '/reports',
        component: () => import('@/views/report/DataReport')
      }
    ]
  }
]

const router = new VueRouter({
  routes
})
// 挂载路由导航守卫
router.beforeEach((to, from, next) => {
  if (to.path === '/login') return next()
  const tokenStr = getItem('token')
  if (!tokenStr) return next('/login')
  next()
})

export default router
